home *** CD-ROM | disk | FTP | other *** search
/ The Programmer Disk / The Programmer Disk (Microforum).iso / xpro / vc / pro4 / wdemo.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-03  |  13.7 KB  |  518 lines

  1. // ObjectWindows - (C) Copyright 1992 by Borland International
  2.  
  3. #define WIN30
  4.  
  5. #include <owl.h>
  6. #include <dialog.h>
  7. #include <edit.h>
  8. #include <stdlib.h>
  9. #include <dclass.hpp>
  10. #include <string.h>
  11. #include <checkbox.h>
  12. #include "wdemo.hpp"
  13.  
  14. // ********** Create Pointers to dClass Objects *********
  15. DBF *testdbf;
  16. NTX *testntx;
  17. NDX *testndx;
  18. // ****************** End dClass code *******************
  19.  
  20. // ********** Field Descriptions for File Create ********
  21. struct FIELD_INFO test_fields[]={
  22.     {"NAME",        'C',    25,    0},
  23.    {"ADDRESS",     'C',    25,    0},
  24.    {"CITY",        'C',    15,    0},
  25.    {"STATE",       'C',     2,    0},
  26.     {"ZIP",         'C',     5,    0},
  27.    {"AGE",             'C',         3,     0}, 
  28.    {"SALARY",      'N',     9,    2}};
  29. // ****************** End dClass code *******************
  30.  
  31. // ******** Index Expression Evaluation Function ********
  32. int FAR PASCAL make_key (NTX &n)
  33. {        
  34.     char keybuffer[40];
  35.    strcpy (n.keydata, n.rtn_dbf()->getfld ("name", keybuffer));
  36.     return (0);
  37. }
  38.  
  39. int FAR PASCAL make_key (NDX &n)
  40. {        
  41.     char keybuffer[40];
  42.     strcpy (n.keydata, n.rtn_dbf()->getfld ("name", keybuffer));
  43.     return (0);
  44. }
  45. // ****************** End dClass code *******************
  46.  
  47. struct TTransferBuf
  48. {
  49.       char Name[26];
  50.       char Address[26];
  51.       char City[16];
  52.       char State[3];
  53.       char Zip[11];
  54.       char Age[4];
  55.     char Salary[10];
  56. };
  57.  
  58. struct TTransferBuf2
  59. {
  60.       char Name[26];
  61. };
  62.  
  63. class TDemoDialog : public TDialog
  64. {
  65. public:
  66.       TDemoDialog(PTWindowsObject AParent, LPSTR AName);
  67.       virtual void NewSaveButtonMsg(RTMessage Msg)
  68.         = [ID_FIRST + ID_NEWSAVE];
  69.       virtual void TopButtonMsg(RTMessage Msg)
  70.          = [ID_FIRST + ID_TOP];
  71.       virtual void BackButtonMsg(RTMessage Msg)
  72.         = [ID_FIRST + ID_BACK];
  73.       virtual void ForwardButtonMsg(RTMessage Msg)
  74.          = [ID_FIRST + ID_FORWARD];
  75.       virtual void SaveButtonMsg(RTMessage Msg)
  76.          = [ID_FIRST + ID_SAVE];
  77.       virtual void BottomButtonMsg(RTMessage Msg)
  78.         = [ID_FIRST + ID_BOTTOM];
  79.     void InitControls();
  80. };
  81.  
  82. class TSeekDialog : public TDialog
  83. {
  84. protected:
  85.     int Exact;
  86. public:
  87.       TSeekDialog(PTWindowsObject AParent, LPSTR AName);
  88.     virtual void SeekMsg(RTMessage Msg)
  89.         = [ID_FIRST + ID_SEEK];
  90.     virtual void SeekExactMsg(RTMessage Msg)
  91.         = [ID_FIRST + ID_SEEKEXACT];
  92.     void InitControls();
  93. };
  94.  
  95. class TDemoWindow : public TWindow
  96. {
  97. protected:
  98.     int Found;
  99. public:
  100.     TTransferBuf DataBuf;
  101.    TTransferBuf2 DataBuf2;
  102.       TDemoWindow(PTWindowsObject AParent, LPSTR ATitle);
  103.     void EraseBuffer();
  104.    void EraseBuffer2();
  105.       virtual void CMEdit(RTMessage Msg)
  106.          = [CM_FIRST + CM_EDIT];
  107.       virtual void CMAppend(RTMessage Msg)
  108.          = [CM_FIRST + CM_APPEND];
  109.     virtual void CMSeek(RTMessage Msg)
  110.         = [CM_FIRST + CM_SEEK];
  111.     virtual void CMDClass(RTMessage Msg)
  112.          = [CM_FIRST + CM_DCLASS];
  113.       virtual void CMDemo(RTMessage Msg)
  114.          = [CM_FIRST + CM_DEMO];
  115.       virtual void CMQuit(RTMessage Msg)
  116.         = [CM_FIRST + CM_QUIT];
  117.     void GetFields();
  118.    void PutFields();
  119.     void SetFound (int found) { Found = found; };
  120. };
  121.  
  122. class TDemoApp : public TApplication
  123. {
  124. public:
  125.   TDemoApp(LPSTR AName, HINSTANCE hInstance, HINSTANCE hPrevInstance,
  126.     LPSTR lpCmdLine, int nCmdShow) :
  127.     TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
  128.   virtual void InitMainWindow();
  129. };
  130.  
  131. char * strim (char *string)
  132. {
  133.     for (int i=strlen (string)-1; i>=0; i--)
  134.         if (string[i] != ' ')
  135.             {
  136.             string [++i] = '\0';
  137.             return (string);
  138.             }
  139.     string[0] = '\0';
  140.     return (string);
  141. }
  142.     
  143. TDemoDialog::TDemoDialog(PTWindowsObject AParent, LPSTR AName)
  144.   : TDialog(AParent, AName)
  145. {
  146.     InitControls();
  147. }
  148.  
  149. TSeekDialog::TSeekDialog(PTWindowsObject AParent, LPSTR AName)
  150.   : TDialog(AParent, AName)
  151. {
  152.     Exact = 0;
  153.       InitControls();
  154. }
  155.  
  156. void TDemoDialog::NewSaveButtonMsg(RTMessage)
  157. {
  158.     TDemoWindow * DWPTR = (TDemoWindow *)GetApplication()->MainWindow;
  159.     TransferData (TF_GETDATA);
  160.  
  161. // ****** Get Data from Transfer Buffer and Append ******
  162.     DWPTR->PutFields();
  163.     DCWORD resp = testdbf->append();
  164. // ****************** End dClass code *******************
  165.     if (resp)
  166.          MessageBox (NULL,"Error Appending Record","Error",MB_OK);
  167.  
  168.     DWPTR->EraseBuffer();
  169.     TransferData (TF_SETDATA);
  170.     SetFocus (GetDlgItem (HWindow, ID_NAME));
  171. }
  172.  
  173. void TDemoDialog::SaveButtonMsg(RTMessage)
  174. {
  175.     TDemoWindow * DWPTR = (TDemoWindow *)GetApplication()->MainWindow;
  176.     TransferData (TF_GETDATA);
  177.  
  178. // ******* Get Data from Transfer Buffer and Write ******
  179.     DWPTR->PutFields();
  180.     DCWORD resp = testdbf->putrec();
  181. // ****************** End dClass code *******************
  182.     if (resp)
  183.         MessageBox (NULL,"Error Writing Record","Error",MB_OK);
  184.  
  185.     SetFocus (GetDlgItem (HWindow, ID_NAME));
  186. }
  187.  
  188. void TDemoDialog::TopButtonMsg(RTMessage)
  189. {
  190.     TDemoWindow * DWPTR = (TDemoWindow *)GetApplication()->MainWindow;
  191.  
  192. // ***** Go to Top of File and Set Transfer Buffer ******
  193.     testntx->top();
  194.     DWPTR->GetFields();
  195. // ****************** End dClass code *******************
  196.  
  197.     strim ((char *)DWPTR->DataBuf.Name);
  198.     strim ((char *)DWPTR->DataBuf.Address);
  199.     strim ((char *)DWPTR->DataBuf.City);
  200.     strim ((char *)DWPTR->DataBuf.State);
  201.     strim ((char *)DWPTR->DataBuf.Zip);
  202.     strim ((char *)DWPTR->DataBuf.Age);
  203.     strim ((char *)DWPTR->DataBuf.Salary);
  204.     TransferData (TF_SETDATA);
  205.     SetFocus (GetDlgItem (HWindow, ID_NAME));
  206. }
  207.  
  208. void TDemoDialog::BackButtonMsg(RTMessage)
  209. {
  210. // ****** If Already at Beginning of File, Return *******
  211.     if (testntx->rtn_bof())
  212. // ****************** End dClass code *******************
  213.         {
  214.       MessageBeep (0);
  215.         SetFocus (GetDlgItem (HWindow, ID_NAME));
  216.         return;
  217.       }
  218.  
  219.     TDemoWindow * DWPTR = (TDemoWindow *)GetApplication()->MainWindow;
  220.  
  221. // **** Skip Back One Record and Set Transfer Buffer ****
  222.     testntx->skip(-1L);
  223.     DWPTR->GetFields();
  224. // ****************** End dClass code *******************
  225.  
  226.     strim ((char *)DWPTR->DataBuf.Name);
  227.     strim ((char *)DWPTR->DataBuf.Address);
  228.     strim ((char *)DWPTR->DataBuf.City);
  229.     strim ((char *)DWPTR->DataBuf.State);
  230.     strim ((char *)DWPTR->DataBuf.Zip);
  231.     strim ((char *)DWPTR->DataBuf.Age);
  232.     strim ((char *)DWPTR->DataBuf.Salary);
  233.     TransferData (TF_SETDATA);
  234.     SetFocus (GetDlgItem (HWindow, ID_NAME));
  235. }
  236.  
  237. void TDemoDialog::ForwardButtonMsg(RTMessage)
  238. {
  239. // ********* If Already at End of File, Return **********
  240.     if (testntx->rtn_eof())
  241. // ****************** End dClass code *******************
  242.         {
  243.       MessageBeep (0);
  244.         SetFocus (GetDlgItem (HWindow, ID_NAME));
  245.         return;
  246.       }
  247.  
  248.     TDemoWindow * DWPTR = (TDemoWindow *)GetApplication()->MainWindow;
  249.  
  250. // *** Skip Forward One Record and Set Transfer Buffer **
  251.     testntx->skip();
  252.     DWPTR->GetFields();
  253. // ****************** End dClass code *******************
  254.  
  255.     strim ((char *)DWPTR->DataBuf.Name);
  256.     strim ((char *)DWPTR->DataBuf.Address);
  257.     strim ((char *)DWPTR->DataBuf.City);
  258.     strim ((char *)DWPTR->DataBuf.State);
  259.     strim ((char *)DWPTR->DataBuf.Zip);
  260.     strim ((char *)DWPTR->DataBuf.Age);
  261.     strim ((char *)DWPTR->DataBuf.Salary);
  262.     TransferData (TF_SETDATA);
  263.     SetFocus (GetDlgItem (HWindow, ID_NAME));
  264. }
  265.  
  266. void TDemoDialog::BottomButtonMsg(RTMessage)
  267. {
  268.     TDemoWindow * DWPTR = (TDemoWindow *)GetApplication()->MainWindow;
  269.  
  270. // **** Go to Bottom of File and Set Transfer Buffer ****
  271.     testntx->bottom();
  272.     DWPTR->GetFields();
  273. // ****************** End dClass code *******************
  274.  
  275.     strim ((char *)DWPTR->DataBuf.Name);
  276.     strim ((char *)DWPTR->DataBuf.Address);
  277.     strim ((char *)DWPTR->DataBuf.City);
  278.     strim ((char *)DWPTR->DataBuf.State);
  279.     strim ((char *)DWPTR->DataBuf.Zip);
  280.     strim ((char *)DWPTR->DataBuf.Age);
  281.     strim ((char *)DWPTR->DataBuf.Salary);
  282.     TransferData (TF_SETDATA);
  283.     SetFocus (GetDlgItem (HWindow, ID_NAME));
  284. }
  285.  
  286. void TSeekDialog::SeekExactMsg(RTMessage)
  287. {
  288.     Exact = (Exact ? 0 : 1);
  289. }
  290.  
  291. void TSeekDialog::SeekMsg(RTMessage)
  292. {
  293.     long rtn;
  294.     char seekstr[26];
  295.  
  296.     memset (seekstr, ' ', 25);
  297.     seekstr[25] = '\0';
  298.  
  299.     TDemoWindow * DWPTR = (TDemoWindow *)GetApplication()->MainWindow;
  300.     TransferData (TF_GETDATA);
  301.    DWPTR->SetFound (FALSE);
  302.     memcpy (seekstr, (char *)DWPTR->DataBuf2.Name,
  303.         strlen ((char *)DWPTR->DataBuf2.Name));
  304.  
  305. // ******** Seek for the Record in the Data File ********
  306.     rtn = testntx->seek (seekstr, Exact);
  307. // ****************** End dClass code *******************
  308.  
  309.     if (!rtn)
  310.         {
  311.         char buf[40];
  312.       sprintf (buf, "seek returned %d", rtn); 
  313.         MessageBox (NULL, buf, "Seek Failed", MB_OK);
  314.       }
  315.     else
  316.         {
  317.         DWPTR->SetFound (TRUE);
  318.         CloseWindow (IDCANCEL);
  319.       }
  320. }
  321.  
  322. TDemoWindow::TDemoWindow(PTWindowsObject AParent, LPSTR ATitle)
  323.   : TWindow(AParent, ATitle)
  324. {
  325.   AssignMenu("MENU_1");
  326.   Attr.X = GetSystemMetrics(SM_CXSCREEN) / 8;
  327.   Attr.Y = GetSystemMetrics(SM_CYSCREEN) / 8;
  328.   Attr.H = Attr.Y * 6;
  329.   Attr.W = Attr.X * 6;
  330.   memset(&DataBuf, 0, sizeof(TTransferBuf));
  331. }
  332.  
  333. void TDemoWindow::EraseBuffer()
  334. {
  335.       DataBuf.Name[0] = NULL;
  336.       DataBuf.Address[0] = NULL;
  337.       DataBuf.City[0] = NULL;
  338.       DataBuf.State[0] = NULL;
  339.       DataBuf.Zip[0] = NULL;
  340.       DataBuf.Age[0] = NULL;
  341.     DataBuf.Salary[0] = NULL;
  342. }
  343.  
  344. void TDemoWindow::EraseBuffer2()
  345. {
  346.       DataBuf2.Name[0] = NULL;
  347. }
  348.  
  349. void TDemoWindow::CMEdit(RTMessage)
  350. {
  351.       PTDialog PD;
  352.  
  353. // ***** If No Records in Database, Error and Return ****
  354.     if (!testdbf->rtn_record_count())
  355. // ****************** End dClass code *******************
  356.         {
  357.         MessageBox (NULL, "No Records to Edit", "Edit Error", MB_OK);
  358.         return;
  359.       }
  360.  
  361.     PD = new TDemoDialog(this, "DIALOG_1");
  362.     PD->SetTransferBuffer(&DataBuf);
  363.  
  364. // ** Get the Current Record and Put in Transfer Buffer *
  365.     GetFields();
  366. // ****************** End dClass code *******************
  367.  
  368.     strim ((char *)DataBuf.Name);
  369.     strim ((char *)DataBuf.Address);
  370.     strim ((char *)DataBuf.City);
  371.     strim ((char *)DataBuf.State);
  372.     strim ((char *)DataBuf.Zip);
  373.     strim ((char *)DataBuf.Age);
  374.     strim ((char *)DataBuf.Salary);
  375.     PD->TransferData (TF_SETDATA);
  376.       GetApplication()->ExecDialog(PD);
  377. }
  378.  
  379. void TDemoWindow::CMAppend(RTMessage)
  380. {
  381.   PTDialog PD;
  382.   EraseBuffer();
  383.   PD = new TDemoDialog(this, "DIALOG_4");
  384.   PD->SetTransferBuffer(&DataBuf);
  385.   PD->TransferData (TF_SETDATA);
  386.   GetApplication()->ExecDialog(PD);
  387. }
  388.  
  389. void TDemoWindow::CMSeek(RTMessage)
  390. {
  391.     PTDialog PD, PD2;
  392.  
  393. // ***** If No Records in Database, Error and Return ****
  394.     if (!testdbf->rtn_record_count())
  395. // ****************** End dClass code *******************
  396.         {
  397.         MessageBox (NULL, "No Records to Seek", "Edit Error", MB_OK);
  398.         return;
  399.       }
  400.  
  401.     Found = FALSE;
  402.       EraseBuffer2();
  403.       PD = new TSeekDialog(this, "DIALOG_5");
  404.     PD->SetTransferBuffer(&DataBuf2);
  405.       PD->TransferData (TF_SETDATA);
  406.     GetApplication()->ExecDialog(PD);
  407.     int found = Found;
  408.     if (found)
  409.        {
  410.           PD2 = new TDemoDialog(this, "DIALOG_1");
  411.         PD2->SetTransferBuffer(&DataBuf);
  412.  
  413. // *** Get the First Record and Put in Transfer Buffer **
  414.         if (testdbf->rtn_record_count())
  415.            {
  416.             GetFields();
  417. // ****************** End dClass code *******************
  418.  
  419.             strim ((char *)DataBuf.Name);
  420.             strim ((char *)DataBuf.Address);
  421.             strim ((char *)DataBuf.City);
  422.             strim ((char *)DataBuf.State);
  423.             strim ((char *)DataBuf.Zip);
  424.             strim ((char *)DataBuf.Age);
  425.             strim ((char *)DataBuf.Salary);
  426.             }
  427.         PD2->TransferData (TF_SETDATA);
  428.         GetApplication()->ExecDialog(PD2);
  429.       }
  430. }
  431.  
  432. void TDemoWindow::CMDClass(RTMessage)
  433. {
  434.   GetApplication()->ExecDialog(new TDialog(this, "DIALOG_2"));
  435. }
  436.  
  437. void TDemoWindow::CMDemo(RTMessage)
  438. {
  439.   GetApplication()->ExecDialog(new TDialog(this, "DIALOG_3"));
  440. }
  441.  
  442. void TDemoWindow::CMQuit(RTMessage)
  443. {
  444.       SendMessage (HWindow, WM_SYSCOMMAND, SC_CLOSE, 0L);
  445. }
  446.  
  447. void TDemoWindow::GetFields()
  448. {
  449. // ****** Get Field Data and Put in Transfer Buffer *****
  450.     testdbf->getfld ("name",     (char *)DataBuf.Name);
  451.     testdbf->getfld ("address", (char *)DataBuf.Address);
  452.     testdbf->getfld ("city",     (char *)DataBuf.City);
  453.     testdbf->getfld ("state",     (char *)DataBuf.State);
  454.     testdbf->getfld ("zip",         (char *)DataBuf.Zip);
  455.     testdbf->getfld ("age",         (char *)DataBuf.Age);
  456.     testdbf->getfld ("salary",     (char *)DataBuf.Salary);
  457. // ****************** End dClass code *******************
  458. }
  459.  
  460. void TDemoWindow::PutFields()
  461. {
  462. // *** Get Data from Transfer Buffer and Put in Fields **
  463.     testdbf->putfld ("name",     (char *)DataBuf.Name);
  464.     testdbf->putfld ("address", (char *)DataBuf.Address);
  465.     testdbf->putfld ("city",     (char *)DataBuf.City);
  466.     testdbf->putfld ("state",     (char *)DataBuf.State);
  467.     testdbf->putfld ("zip",         (char *)DataBuf.Zip);
  468.     testdbf->putfld ("age",         (char *)DataBuf.Age);
  469.     testdbf->putfld ("salary",     (char *)DataBuf.Salary);
  470. // ****************** End dClass code *******************
  471. }
  472.  
  473. void TDemoApp::InitMainWindow()
  474. {
  475.   MainWindow = new TDemoWindow(NULL, Name);
  476. }
  477.  
  478. void TDemoDialog::InitControls()
  479. {
  480.     new TEdit(this, ID_NAME, 26);
  481.       new TEdit(this, ID_ADDRESS, 26);
  482.       new TEdit(this, ID_CITY, 16);
  483.       new TEdit(this, ID_STATE, 3);
  484.       new TEdit(this, ID_ZIP, 11);
  485.       new TEdit(this, ID_AGE, 4);
  486.       new TEdit(this, ID_SALARY, 11);
  487. }
  488.  
  489. void TSeekDialog::InitControls()
  490. {
  491.     new TEdit(this, ID_NAME, 26);
  492. }
  493.  
  494. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  495.   LPSTR lpCmdLine, int nCmdShow)
  496. {
  497.       TDemoApp TestApp("dClass Demo", hInstance, hPrevInstance,
  498.         lpCmdLine, nCmdShow);
  499.  
  500. // ************** Create the dClass Objects *************
  501.     testdbf = new DBF ("testfile", 7, test_fields);
  502.     testntx = new NTX ("testfile", testdbf, 25, "name", 0, 0,
  503.         IO_BUFFER_SIZE, make_key);
  504.     testndx = new NDX ("testfile", testdbf, 25, 0, "name", 0, 0,
  505.         IO_BUFFER_SIZE, make_key);
  506. // ****************** End dClass code *******************
  507.  
  508.     TestApp.Run();
  509.  
  510. // ************** Delete the dClass Objects *************
  511.     delete testdbf;
  512.     delete testntx;
  513.    delete testndx;
  514. // ****************** End dClass code *******************
  515.  
  516.     return (TestApp.Status);
  517. }
  518.